package cloud.coder.remote.test;

import cloud.coder.remote.boot.BootService;
import cloud.coder.remote.boot.ServiceManager;
import cloud.coder.remote.grpc.client.service.HelloServiceClient;
import cloud.coder.remote.grpc.client.service.MsgReportServiceClient;
import com.google.common.collect.Lists;
import io.grpc.examples.msg.report.MsgCollection;
import io.grpc.examples.msg.report.MsgObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.ServiceLoader;
import java.util.concurrent.TimeUnit;

public class ClientMain {
    private static final Logger LOGGER = LoggerFactory.getLogger(ClientMain.class);

    public static void main(String[] args) throws Exception {
        try {
            ServiceManager.INSTANCE.boot();
        } catch (Exception e) {
            LOGGER.error("boot failure.", e);
        }

        Runtime.getRuntime()
                .addShutdownHook(
                        new Thread(ServiceManager.INSTANCE::shutdown, "service shutdown thread"));

        HelloServiceClient helloServiceClient = ServiceManager.INSTANCE.findService(HelloServiceClient.class);
        for (int i = 0; i < 10; i++) {
            helloServiceClient.greet("张三" + i);
        }

        MsgReportServiceClient msgReportServiceClient = ServiceManager.INSTANCE.findService(MsgReportServiceClient.class);
        MsgCollection.Builder msgCollection = MsgCollection.newBuilder();
        for(int i=0;i<5;i++){
            MsgObject.Builder builder = MsgObject.newBuilder();
            builder.setId(i+"");
            builder.setMsg("report msg "+i);
            builder.setType(i+"");
            msgCollection.addMsgs(builder);
        }
        msgReportServiceClient.reportInSync(msgCollection.build());

        List<MsgObject> msgs = Lists.newArrayList();
        for(int i=10;i<15;i++){
            MsgObject.Builder builder = MsgObject.newBuilder();
            builder.setId(i+"");
            builder.setMsg("report msg "+i);
            builder.setType(i+"");
            msgs.add(builder.build());
        }
        msgReportServiceClient.reportBySteam(msgs);

        TimeUnit.SECONDS.sleep(1000);
    }
}
